package com.qtjf.qtmoneyservice.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;

/**
 * Created by Administrator on 2017/7/3 0003.
 */

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
        private Canvas mCanvas;
        private boolean isDrawing;
        Paint mPaint;

public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        init();
        }

        private void init() {
                mHolder = getHolder();
                mHolder.addCallback(this);
                mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
                setFocusable(true);
                setFocusableInTouchMode(true);
                this.setKeepScreenOn(true);
                mPaint=new Paint();
                mPaint.setColor(Color.YELLOW);
                mPaint.setStrokeWidth(5);
                mPaint.setStyle(Paint.Style.STROKE);
        }

        public void surfaceCreated(final SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
               /* new Thread(new Runnable() {
                        @Override
                        public void run() {
                                mCanvas=holder.lockCanvas();
                                mCanvas.drawRect(0,0,200,100,mPaint);
                                holder.unlockCanvasAndPost(mCanvas);
                        }
                }).start();*/
        try {
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
                isDrawing=true;
             /*   mCanvas=mHolder.lockCanvas();
                mCanvas.drawRect(0,0,400,200,mPaint);
                mHolder.unlockCanvasAndPost(mCanvas);*/
        } catch (IOException e) {
                Log.d("error", "Error setting camera preview: " + e.getMessage());
        }
        }

public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
        /*if (mCamera!=null) {
                mCamera.release();
                mCamera=null;
        }*/
        isDrawing=false;
        }

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
        // preview surface does not exist
        return;
        }

        // stop preview before making changes
        try {
        mCamera.stopPreview();
        } catch (Exception e){
        // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();

        } catch (Exception e){
        Log.d("error", "Error starting camera preview: " + e.getMessage());
        }
        }

       /* @Override
        protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                canvas.drawRect(0,0,400,200,mPaint);
                Log.d("eeee","1111");
        }*/
}

